home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 October: Windmill on DISC / ADC Developer CD (1993-10) (''Windmill On DISC'')_iso / Dev.CD Oct 93.iso / System Software / U.S. System Software / System 7 Pro™ Beta 11 / Development Tools / Sample Code / Messaging Service Access Module / Internet PMSAM / Internet PMSAM source / spooltoexternal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-27  |  5.5 KB  |  228 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------
  2.  
  3. AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
  4. Mail Service Access Module
  5.  
  6. written by Steve Falkenburg-- MacDTS
  7. ©1991-1993 Apple Computer, Inc.
  8.  
  9. --------------
  10. change history
  11. --------------
  12.  
  13. SJF        02/19/93    update for beta build    b1
  14. SJF        10/29/92    update to a11            a11
  15. SJF        06/08/92    update to a8            a8
  16. SJF        02/15/92    first working version    a4.5
  17. SJF        10/16/91    initial coding            a3
  18.  
  19. ---------------------------------------------------------------------*/
  20.  
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24.  
  25. #ifndef __OCE__
  26. #include <OCE.h>
  27. #endif
  28.  
  29. #ifndef __OCEMAIL__
  30. #include <OCEMail.h>
  31. #endif
  32.  
  33. #include <string.h>
  34.  
  35. #include "const.h"
  36. #include "gwerrors.h"
  37. #include "mytypes.h"
  38. #include "globals.h"
  39. #include "utils.h"
  40. #include "spoolsystem.h"
  41. #include "gatewaystuff.h"
  42. #include "smtp.protocol.h"
  43. #include "convertaddress.h"
  44.  
  45. #include "spooltoexternal.h"
  46.  
  47. #define    kMaxMsgSize    64000
  48. #define    kBCCBlockSize 2048
  49.  
  50. #define    kFromHeader    "From: "
  51. #define    kToHeader    "To: "
  52. #define    kCCHeader    "Cc: "
  53. #define    kBCCHeader    "Bcc: "
  54. #define    kSubjectHeader "Subject: "
  55. #define    kAddressDelimiter ", "
  56.  
  57. OSErr SpoolToExternalGW(FSSpec *spoolSpec,SlotSpec *slotSpec)
  58. {
  59.     unsigned long smtpServerAddress;
  60.     OSErr err;
  61.     char tmpString[256];
  62.     char bccBlock[256];
  63.     char fromAddr[256];
  64.     char *textBlock;
  65.     char *messageBlock;
  66.     unsigned long textBlockLen,textBlockStart,tmpLen;
  67.     RString tmpRString;
  68.     char packedRecip[kMaxRecipSize];
  69.     short recipIndex;
  70.     Boolean hasRecipient;
  71.     
  72.     // allocate memory
  73.     
  74.     messageBlock = (char *)NewPtrChk(kMaxMsgSize);
  75.     if (MemError()!=noErr) {
  76.         return BailOnSend(MemError(),nil);
  77.     }
  78.     messageBlock[0] = 0;
  79.         
  80.     // get IP address of SMTP server
  81.     
  82.     err = ConvertStringToAddr(slotSpec->specInfo.smtpServer,&smtpServerAddress);
  83.     if (err!=noErr) {
  84.         return BailOnSend(kInvalidSMTPServer,messageBlock);
  85.     }
  86.         
  87.     // build from address (from POP account)
  88.     
  89.     strcpy(fromAddr,slotSpec->dirIdentity.userName);
  90.     strcat(fromAddr,"@");
  91.     strcat(fromAddr,slotSpec->specInfo.popServer);
  92.     strcpy(messageBlock,kFromHeader);
  93.     strcat(messageBlock,fromAddr);
  94.     strcat(messageBlock,kCRStr);
  95.  
  96.     // build to address
  97.     
  98.     hasRecipient = false;
  99.     strcat(messageBlock,kToHeader);
  100.     for (err=noErr,recipIndex=0; err==noErr; recipIndex++) {
  101.         tmpLen = kMaxRecipSize;
  102.         err = GetFromSpool(spoolSpec,kToType,kAddrCreator,recipIndex,(Ptr)packedRecip,&tmpLen,0);
  103.         if (err==noErr) {
  104.             if (TranslateAddress((OCEPackedRecipient *)packedRecip,tmpString)) {
  105.                 strcat(messageBlock,tmpString);
  106.                 strcat(messageBlock,kAddressDelimiter);
  107.                 hasRecipient = true;
  108.             }
  109.         }
  110.     }
  111.     if (hasRecipient) {
  112.         messageBlock[strlen(messageBlock)-strlen(kAddressDelimiter)] = 0;
  113.         strcat(messageBlock,kCRStr);
  114.     }
  115.     else {
  116.         messageBlock[strlen(messageBlock)-strlen(kToHeader)] = 0;
  117.     }
  118.  
  119.     // build cc address
  120.     
  121.     hasRecipient = false;
  122.     strcat(messageBlock,kCCHeader);
  123.     for (err=noErr,recipIndex=0; err==noErr; recipIndex++) {
  124.         tmpLen = kMaxRecipSize;
  125.         err = GetFromSpool(spoolSpec,kCCType,kAddrCreator,recipIndex,(Ptr)packedRecip,&tmpLen,0);
  126.         if (err==noErr) {
  127.             if (TranslateAddress((OCEPackedRecipient *)packedRecip,tmpString)) {
  128.                 strcat(messageBlock,tmpString);
  129.                 strcat(messageBlock,kAddressDelimiter);
  130.                 hasRecipient = true;
  131.             }
  132.         }
  133.     }
  134.     if (hasRecipient) {
  135.         messageBlock[strlen(messageBlock)-strlen(kAddressDelimiter)] = 0;
  136.         strcat(messageBlock,kCRStr);
  137.     }
  138.     else {
  139.         messageBlock[strlen(messageBlock)-strlen(kCCHeader)] = 0;
  140.     }
  141.     
  142.     // build bcc address (and store in separate string)
  143.     
  144.     hasRecipient = false;
  145.     strcpy(bccBlock,kBCCHeader);
  146.     for (err=noErr,recipIndex=0; err==noErr; recipIndex++) {
  147.         tmpLen = kMaxRecipSize;
  148.         err = GetFromSpool(spoolSpec,kBCCType,kAddrCreator,recipIndex,(Ptr)packedRecip,&tmpLen,0);
  149.         if (err==noErr) {
  150.             if (TranslateAddress((OCEPackedRecipient *)packedRecip,tmpString)) {
  151.                 strcat(bccBlock,tmpString);
  152.                 strcat(bccBlock,kAddressDelimiter);
  153.                 hasRecipient = true;
  154.             }
  155.         }
  156.     }
  157.     if (hasRecipient) {
  158.         messageBlock[strlen(messageBlock)-strlen(kAddressDelimiter)] = 0;
  159.         strcat(bccBlock,kCRStr);
  160.     }
  161.  
  162.     // build subject
  163.         
  164.     strcat(messageBlock,kSubjectHeader);
  165.     tmpLen = kRStringMaxBytes;
  166.     err = GetFromSpool(spoolSpec,kSubjectType,kAttribCreator,0,(Ptr)&tmpRString,&tmpLen,0);
  167.     if (err==noErr) {
  168.         r2cString(&tmpRString,tmpString);
  169.     }
  170.     else
  171.         strcpy(tmpString,"<none>");
  172.     strcat(messageBlock,tmpString);
  173.     strcat(messageBlock,kCRStr);
  174.     strcat(messageBlock,kCRStr);
  175.  
  176.     // end message header
  177.         
  178.     textBlockStart = strlen(messageBlock);
  179.     textBlock = messageBlock+textBlockStart;
  180.     
  181.     // build body
  182.     
  183.     textBlockLen = kMaxMsgSize-textBlockStart;
  184.     err = GetFromSpool(spoolSpec,kTextContent,kContentCreator,0,textBlock,&textBlockLen,0);
  185.     if (err!=noErr)
  186.         return BailOnSend(err,messageBlock);
  187.     textBlock[textBlockLen] = 0;
  188.     
  189.     err = SendSMTP(messageBlock,bccBlock,fromAddr,smtpServerAddress);
  190.     if (err!=noErr) {
  191.         return BailOnSend(kInvalidSMTPServer,messageBlock);
  192.     }
  193.     
  194.     DisposPtrChk(messageBlock);
  195.     return noErr;
  196. }
  197.  
  198.  
  199. OSErr BailOnSend(OSErr err,Ptr messageBlock)
  200. {
  201.     if (messageBlock)
  202.         DisposPtrChk(messageBlock);
  203.  
  204.     return err;
  205. }
  206.  
  207.  
  208. Boolean TranslateAddress(OCEPackedRecipient *pRecip,char *unixRecip)
  209. {
  210.     OCERecipient rcpt;
  211.     RecordID entitySpecifier;
  212.     OSType recipType;
  213.     RString recipRStr;
  214.     
  215.     OCEUnpackDSSpec((PackedDSSpec*)pRecip,&rcpt,&entitySpecifier);
  216.     recipType = rcpt.extensionType;
  217.     switch (recipType) {
  218.         case kPopAddrType:
  219.             BlockMove(rcpt.extensionValue,&recipRStr,rcpt.extensionSize);    // should probably do some range check
  220.             r2cString(&recipRStr,unixRecip);
  221.             break;
  222.         default: // punt on string representation of aoce addresses for now
  223.             return false;
  224.             break;
  225.     }
  226.     return true;
  227. }
  228.